package com.fingard.dsp.bank.directbank.dbs01;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.dsp.bank.directbank.chanpay01.chanpay01Util.S;
import com.fingard.text.StringHelper;

/**
 * Created by F on 2018/7/12.
 */
public class DBS01ScheduleTask extends DBS01Base implements IScheduleTask {
    @Override
    public void execTask(TaskTimerSet timerSet) {
        if (timerSet.taskNameList.toLowerCase().contains("dbsdownload")) {
            try {
                synchronized (DBS01Base.class) {
                    String decryptPath = getBankConfig().getValueAsString("DecryptPath") + File.separator;
                    List<String> fileList = downloadFile();
//                    List<String> fileList = new ArrayList<>();
                    if (fileList != null && fileList.size() != 0) {
                        for (String filename : fileList) {
                            decryptFile(filename);
                        }
                    } else {
                        String receiveFilePath = getBankConfig().getValueAsString("ReceiveFilePath");
                        String[] receiveFileList = new File(receiveFilePath).list();
                        if(receiveFileList!=null&&receiveFileList.length!=0) {
                            for (String fileName : receiveFileList) {
                                boolean success = false;
                                success = decryptFile(fileName);
                                if (success) {
                                    //统计解密成功的文件
                                    fileList.add(fileName);
                                }
                            }
                        }
                    }
                    //对解密成功的文件进行分析，对分析之后的文件移动到相应的目录
                    if (fileList != null && fileList.size() != 0) {
                        for (String filename : fileList) {
                            analysisFile(filename);

                        }
                    } else {
                        String[] decryptFileList  = new File(decryptPath).list();
                        if (decryptFileList != null && decryptFileList.length != 0) {
                            for (String fileName : new File(decryptPath).list()) {
                                analysisFile(fileName);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
